<?php
namespace Biz\service\theParagraph;

use Biz\model\theParagraph\CategoryClassify as mCategoryClassify;
use Biz\service\SearchGoods;
use Doraemon\model\Category as mCategory;
//use Biz\service\search\Goods as searchGoods;
use Biz\service\Goods as sGoods;
use Biz\service\esSearch\Goods as esGoods;
/**
 * @deprecated
 */
class CategoryClassify
{

	public static function admGetAllByParentId($parentId = 0)
	{
		$classifyInfos = mCategoryClassify::getAllByParentId($parentId);
		$results = [];
		foreach ($classifyInfos as $classifyInfo) {
			if ($classifyInfo['cate_id']) {
				$categoryInfo = mCategory::getCataInfoById($classifyInfo['cate_id']);
				$results[] = [
					'cate_id' => $categoryInfo['id'],
					'cate_name' => $categoryInfo['cate_name'],
				];
			} else {
				$results[$classifyInfo['id']] = [
					'id' => $classifyInfo['id'],
					'classify_name' => $classifyInfo['classify_name'],
					'list' => self::admGetAllByParentId($classifyInfo['id'])
				];
			}
		}
		return $results;
	}

	public static function getClassifiesByIdForApp($id)
	{
		$classifyInfo = mCategoryClassify::getClassifyById($id);
		//不存在  或者不是一级分类
		if (!$classifyInfo || $classifyInfo['parent_id'] != 0) {
			throw new \Exception('不存在的一级分类');
		}

		$Rst = [
			'id' => $classifyInfo['id'],
			'classify_name' => $classifyInfo['classify_name'],
			'sub_classifies' => []
			];
		$secondClassifies = mCategoryClassify::getAllByParentId($classifyInfo['id']);
		foreach ($secondClassifies as $secondClassify) {
			$Rst['sub_classifies'][] = [
				'id' => $secondClassify['id'],
				'classify_name' => $secondClassify['classify_name'],
			];
		}
		return $Rst;
	}

	public static function getGoodsListByClassifyId($secondClassifyId, $sortType, $page)
	{
		$classifyInfo = mCategoryClassify::getClassifyById($secondClassifyId);
		if (!$classifyInfo || $classifyInfo['parent_id'] == 0 || $classifyInfo['cate_id'] != 0) {
			throw new \Exception('不存在的二级分类');
		}
		$classifies = mCategoryClassify::getAllByParentId($secondClassifyId);
		$cateIds = array_column($classifies, 'cate_id');
		$rst = [];

        $results = SearchGoods::searchEntrance([ 'category_id' => array_unique(array_filter($cateIds)), 'source' => esGoods::SOURCE_THE_PARAGRAPH],esGoods::SCENES_THE_PARAGRAPH_TAGS, $sortType, $page, 30);

        if ($results['is_over']) {
            $page = abs($page - floor($results['total'] / 30 ));
            $rows = $results['rows'];
            $results = SearchGoods::searchEntrance(['category_id' => array_filter($cateIds), 'source' => esGoods::SOURCE_THE_PARAGRAPH],esGoods::SCENES_THE_PARAGRAPH_PERFECT, $sortType, $page, 30);
            if($rows) {
                $results['rows'] = array_merge( $rows, $results['rows']);
            }
        }

		$rst['is_over'] = $results['is_over'];
		$rst['list'] = array_values(sGoods::Goods($results['rows'], sGoods::FIELDS_GROUP_LIST));
		return $rst;
	}


}